package com.zhxy.manager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhxy.manager.dao.LibraryDao;
import com.zhxy.manager.dao.SongDao;
import com.zhxy.manager.entity.Library;
import com.zhxy.manager.entity.Song;
import com.zhxy.manager.excel.SongExcel;
import com.zhxy.manager.service.intf.SongService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class SongServiceImpl implements SongService {
    @Autowired
    private SongDao dao;

    @Override
    public R save(Song song) {
        if (dao.add(song)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageBean queryPage(int page, int limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果
        PageInfo<Song> pageInfo = new PageInfo<>(dao.selectAll());
        //3.返回分页的数据(1.获取数据库的总条数，2，返回分页数据)
        return  PageBean.bean(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R del(int id) {
        if(id>0){
            if (dao.del(id)>0){
                return R.ok();
            }
        }
        return  R.fail();
    }

    @Override
    public R update(Song song) {
        if(dao.update(song)>0){
            return R.ok();
        }else {
            return R.fail();
        }

    }

    @Override
    public List<Song> selectAll() {
        return dao.selectAll();
    }

    @Override
    public PageBean queryPageWhere(String name, int page, int limit) {
        //1.验证 查询条件 如果为 "" 那么赋值为null
        if(!StringUtils.hasLength(name)){
            name=null;
        }else{
            name="%"+name+"%";//包含  模糊查询
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<Song> pageInfo=new PageInfo<>(dao.selectWhere(name));
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());

    }


    @Override
    public R delBatch(int[] ids) {
        //1.验证参数
        if(ids!=null && ids.length>0){
            //2.执行
            if(dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    //上传问题  MultipartFile  只要不为空就型穿
    public R saveBatch(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            //监听器new Rea
           List<SongExcel> list=EasyExcel.read(file.getInputStream(), SongExcel.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
           //执行dao 批量新增
            if(dao.addBatch(list)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

}

